package com.wkbb.basic.controller.open;

import com.wkbb.basic.service.XmppService;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.XmppMessageDto;
//import com.wkbb.common.vo.DeviceUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author yxl
 * @date 2019-07-29 10:49
 */
@Api(description = "XMPP接口")
@RestController
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class XmppController {

    @Autowired
    @Lazy
    private XmppService xmppService;

    @ApiOperation(value = "向设备发送Xmpp")
    @RequestMapping(value = "/sendXmpp", method = RequestMethod.POST)
    public ResultDTO sendXmppInOrg(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "发送内容") @RequestBody XmppMessageDto message) {
        log.info("***********XmppController.sendXmpp******apiVersion:{},message:{}", apiVersion, message);
        if (message.getCommand() == null
                || message.getJson() == null
                || (message.getOrgId() == null && (message.getDeviceCodes() == null || message.getDeviceCodes().isEmpty()))) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.sendXmpp(message.getCommand(), message.getOrgId(), message.getDeviceCodes(), message.getJson());

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.sendXmpp******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "重新加载全部用户")
    @RequestMapping(value = "/xmppReloadAllUser", method = RequestMethod.GET)
    public ResultDTO reloadAllUser(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes) {
        log.info("***********XmppController.reloadAllUser******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.reloadAllUser(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.reloadAllUser******", e);
            return new ResultDTO(false);
        }
    }

//    @ApiOperation(value = "同步单个用户信息")
//    @RequestMapping(value = "/xmppUpdatePerson", method = RequestMethod.POST)
//    public ResultDTO updatePerson(
//            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
//            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
//            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes,
//            @ApiParam(value = "是否删除", required = true) @RequestParam(value = "delete", required = true) Boolean delete,
//            @ApiParam(value = "最新用户信息", required = true) @RequestBody DeviceUserVo deviceUser) {
//        log.info("***********XmppController.updatePerson******apiVersion:{},orgId:{},deviceCodes:{},deviceUser:{}", apiVersion, orgId, deviceCodes, deviceUser);
//        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
//            return new ResultDTO(ErrorCode.PARAM_MISSING);
//        }
//
//        try {
//            xmppService.updatePerson(orgId, deviceCodes, delete, deviceUser);
//
//            return new ResultDTO();
//        } catch (Exception e) {
//            log.error("***********XmppController.updatePerson******", e);
//            return new ResultDTO(false);
//        }
//    }

    @ApiOperation(value = "重启考勤软件")
    @RequestMapping(value = "/xmppRestart", method = RequestMethod.GET)
    public ResultDTO restart(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes) {
        log.info("***********XmppController.restart******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.restart(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.restart******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "重启考勤机")
    @RequestMapping(value = "/xmppReboot", method = RequestMethod.GET)
    public ResultDTO reboot(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes) {
        log.info("***********XmppController.reboot******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.reboot(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.reboot******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "上传指定日期日志")
    @RequestMapping(value = "/xmppUpLogs", method = RequestMethod.GET)
    public ResultDTO upLogs(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes,
            @ApiParam(value = "具体日期（yyyy-MM-dd）", required = true) @RequestParam(value = "date", required = true) String date) {
        log.info("***********XmppController.upLogs******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.upLogs(orgId, deviceCodes, date);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.upLogs******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "检查设备连接")
    @RequestMapping(value = "/xmppOnline", method = RequestMethod.GET)
    public ResultDTO online(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes) {
        log.info("***********XmppController.online******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.online(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.online******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "相似度调整")
    @RequestMapping(value = "/xmppSetSimilar", method = RequestMethod.GET)
    public ResultDTO setSimilar(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes,
            @ApiParam(value = "相似度", required = true) @RequestParam(value = "similar", required = true) Integer similar) {
        log.info("***********XmppController.setSimilar******apiVersion:{},orgId:{},deviceCodes:{},similar:{}", apiVersion, orgId, deviceCodes, similar);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.setSimilar(orgId, deviceCodes, similar);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.setSimilar******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "识别距离调整")
    @RequestMapping(value = "/xmppSetLevel", method = RequestMethod.GET)
    public ResultDTO setLevel(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes,
            @ApiParam(value = "识别距离（1—10）", required = true) @RequestParam(value = "level", required = true) Integer level) {
        log.info("***********XmppController.setLevel******apiVersion:{},orgId:{},deviceCodes:{},level:{}", apiVersion, orgId, deviceCodes, level);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.setLevel(orgId, deviceCodes, level);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.setLevel******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "校长设置家长门禁开关")
    @RequestMapping(value = "/xmppSetDoorEnable", method = RequestMethod.GET)
    public ResultDTO setDoorEnable(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes,
            @ApiParam(value = "门禁开关1：默认(家长仅在考勤时间段进出学校) 2：家长全天开放进出 3：家长全天关闭进出", required = true) @RequestParam(value = "enable", required = true) Integer enable) {
        log.info("***********XmppController.setDoorEnable******apiVersion:{},orgId:{},deviceCodes:{},enable:{}", apiVersion, orgId, deviceCodes, enable);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.setDoorEnable(orgId, deviceCodes, enable);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.setDoorEnable******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "清除所有缓存")
    @RequestMapping(value = "/xmppClearCache", method = RequestMethod.GET)
    public ResultDTO clearCache(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes) {
        log.info("***********XmppController.clearCache******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.clearCache(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.clearCache******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "清除所有非crash日志")
    @RequestMapping(value = "/xmppClearLog", method = RequestMethod.GET)
    public ResultDTO clearLog(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes) {
        log.info("***********XmppController.clearLog******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.clearLog(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.clearLog******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "清除老旧日志，直到日志总大小小于1024M")
    @RequestMapping(value = "/xmppClearLogScale", method = RequestMethod.GET)
    public ResultDTO clearLogScale(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes) {
        log.info("***********XmppController.clearLogScale******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.clearLogScale(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.clearLogScale******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "检测主程序和辅助程序是否相连")
    @RequestMapping(value = "/xmppAssistConnect", method = RequestMethod.GET)
    public ResultDTO assistConnect(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes) {
        log.info("***********XmppController.assistConnect******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.assistConnect(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.assistConnect******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "检查版本并更新")
    @RequestMapping(value = "/xmppUpdateCheck", method = RequestMethod.GET)
    public ResultDTO updateCheck(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes) {
        log.info("***********XmppController.updateCheck******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.updateCheck(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.updateCheck******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "通知设备更新并上报配置信息")
    @RequestMapping(value = "/xmppConfigUpdate", method = RequestMethod.GET)
    public ResultDTO configUpdate(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes
//            @ApiParam(value = "人脸最小宽度", required = true) @RequestParam(value = "faceRect", required = true) Integer faceRect,
//            @ApiParam(value = "人脸采样率", required = true) @RequestParam(value = "faceSampling", required = true) Integer faceSampling
            ) {
        log.info("***********XmppController.configUpdate******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.configUpdate(orgId, deviceCodes);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.configUpdate******", e);
            return new ResultDTO(false);
        }
    }
    @ApiOperation(value = "设备红外测温配置信息")
    @RequestMapping(value = "/xmppInfraredUpdate", method = RequestMethod.GET)
    public ResultDTO configUpdate(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(value = "orgId", required = false) Long orgId,
            @ApiParam(value = "设备列表 ,分隔（不填组织ID才有用）", required = false) @RequestParam(value = "deviceCodes", required = false) String deviceCodes,
            @ApiParam(value = "红外测温开关0:关闭1:开启", required = true) @RequestParam(value = "infraredFlag", required = true) Boolean infraredFlag,
            @ApiParam(value = "红外阈值", required = true) @RequestParam(value = "infraredValue", required = true) BigDecimal infraredValue,
            @ApiParam(value = "红外测温补偿值", required = true) @RequestParam(value = "infraredTemperatureCompensation", required = true) BigDecimal infraredTemperatureCompensation
            ) {
        log.info("***********XmppController.configUpdate******apiVersion:{},orgId:{},deviceCodes:{}", apiVersion, orgId, deviceCodes);
        if (orgId == null && StringUtils.isEmpty(deviceCodes)) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.updateInfrared(orgId, deviceCodes, infraredFlag, infraredValue, infraredTemperatureCompensation);

            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.configUpdate******", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "XMPP推送用户信息(用于测试慢sql)")
    @RequestMapping(value = "/xmppUpdatePersonByUserInfo", method = RequestMethod.POST)
    public ResultDTO xmppUpdatePersonByUserInfo(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(value = "orgId", required = true) Long orgId,
            @ApiParam(value = "用户ID", required = false) @RequestParam(value = "userId", required = false) Long userId,
            @ApiParam(value = "角色类型", required = false) @RequestParam(value = "roleType", required = false) Short roleType,
            @ApiParam(value = "时间", required = false) @RequestParam(value = "date", required = false) Date date
    ) {
        log.info("***********XmppController.xmppUpdatePersonByUserInfo******apiVersion:{},orgId:{},userId:{},roleType:{},date:{}", apiVersion, orgId, userId,roleType,date);
        if (orgId == null ) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            xmppService.xmppUpdatePerson(orgId, userId,roleType,date);
            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********XmppController.xmppUpdatePersonByUserInfo******", e);
            return new ResultDTO(false);
        }
    }

}
